

package yf.mall.service.impl.app;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.entity.AppCoursePurchaseEntityExample;
import yf.mall.mapper.AppCoursePurchaseExtMapper;
import yf.mall.service.app.AppCoursePurchaseService;
import yf.mall.vo.up.AppCoursePurchaseUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;

import java.io.InputStream;

import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import yf.mall.vo.down.AppCoursePurchaseDown;

import java.util.List;
import java.net.URLEncoder;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.AppCoursePurchaseDao;
import yf.mall.entity.AppCoursePurchaseEntity;
import yf.mall.vo.AppCoursePurchaseVo;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * 
 *
 * @date Sat Jul 29 17:36:10 CST 2023
 */
@Service
public class AppCoursePurchaseServiceImpl extends BaseServiceImpl<AppCoursePurchaseEntity> implements AppCoursePurchaseService {

    private AppCoursePurchaseDao dao;

    @Resource
    private AppCoursePurchaseExtMapper appCoursePurchaseExtMapper;


    @Autowired
    public void setDao(AppCoursePurchaseDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<AppCoursePurchaseVo> findVo(AppCoursePurchaseVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public AppCoursePurchaseVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AppCoursePurchaseVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AppCoursePurchaseVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<AppCoursePurchaseVo> list) {
        try {
            List<AppCoursePurchaseDown> datas = list.stream().map(item -> new AppCoursePurchaseDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AppCoursePurchaseDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, AppCoursePurchaseUp.class, new UploadDataListener()).sheet().doRead();
    }


    class UploadDataListener extends AnalysisEventListener<AppCoursePurchaseUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(AppCoursePurchaseUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/
    @Override
    public PageInfo<AppCoursePurchaseEntity> page(AppCoursePurchaseEntityExample example, Page<AppCoursePurchaseEntity> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AppCoursePurchaseEntity> entityList = appCoursePurchaseExtMapper.selectByExample(example);
        return new PageInfo(entityList);
    }


}
